package com.mes.loco.aps.server.controller.mcs;

import com.iplant.aps.controller.BaseController;
import com.iplant.aps.service.APSService;
import com.iplant.aps.service.po.Pagination;
import com.iplant.aps.service.po.ServiceResult;
import com.iplant.aps.service.po.bms.BMSEmployee;
import com.iplant.aps.service.po.mcs.MCSLogInfo;
import com.iplant.aps.service.utils.StringUtils;
import com.iplant.aps.utils.RetCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.List;

/**
 * 传输日志控制器
 *
 * @author YouWang·Peng
 * 2021-7-4 14:52:30
 */
@RestController
@RequestMapping("/api/MCSLogInfo")
public class MCSLogInfoController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(MCSLogInfoController.class);

    final
    APSService wAPSService;

    public MCSLogInfoController(APSService wAPSService) {
        this.wAPSService = wAPSService;
    }

    /**
     * 获取传输日志列表
     */
    @GetMapping("/LogList")
    public Object LogList(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            String wProductNo = StringUtils.parseString(request.getParameter("ProductNo"));
            String wLineName = StringUtils.parseString(request.getParameter("LineName"));
            String wCustomerName = StringUtils.parseString(request.getParameter("CustomerName"));
            String wPartNo = StringUtils.parseString(request.getParameter("PartNo"));
            String wFileType = StringUtils.parseString(request.getParameter("FileType"));
            String wVersionNo = StringUtils.parseString(request.getParameter("VersionNo"));

            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));

            int wPageIndex = StringUtils.parseInt(request.getParameter("PageIndex"));
            int wPageSize = StringUtils.parseInt(request.getParameter("PageSize"));
            String wSort = StringUtils.parseString(request.getParameter("Sort"));
            String wSortType = StringUtils.parseString(request.getParameter("SortType"));

            wPageIndex += 1;
            Pagination wPagination = Pagination.Create(wPageIndex, wPageSize, wSort, wSortType);

            ServiceResult<List<MCSLogInfo>> wServiceResult = wAPSService.MCS_QueryLogList(wLoginUser, wProductNo,
                    wLineName, wCustomerName, wPartNo, wFileType, wVersionNo, wStartTime,
                    wEndTime, wPagination);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 查单条
     */
    @GetMapping("/Info")
    public Object Info(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wID = StringUtils.parseInt(request.getParameter("ID"));

            if (wID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, "提示：参数错误，ID不能小于或等于0!");
            }

            ServiceResult<MCSLogInfo> wServiceResult = wAPSService.MCS_QueryLogInfo(wLoginUser, wID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 通过BOMID查询传输日志ID
     */
    @GetMapping("/IDByBOMID")
    public Object IDByBOMID(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wBOMID = StringUtils.parseInt(request.getParameter("BOMID"));

            if (wBOMID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, "提示：参数错误，标准BOMID不能小于或等于0!");
            }

            ServiceResult<Integer> wServiceResult = wAPSService.MCS_QueryIDByBOMID(wLoginUser, wBOMID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 通过BOPID查询传输日志ID
     */
    @GetMapping("/IDByBOPID")
    public Object IDByBOPID(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wBOPID = StringUtils.parseInt(request.getParameter("BOPID"));

            if (wBOPID <= 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, "提示：参数错误，工艺BOPID不能小于或等于0!");
            }

            ServiceResult<Integer> wServiceResult = wAPSService.MCS_QueryIDByBOPID(wLoginUser, wBOPID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }
}
